package com.rzico.sbl.ui.order

import android.widget.ImageView
import androidx.core.view.isGone
import androidx.core.view.isVisible
import androidx.lifecycle.lifecycleScope
import com.amap.api.toAMapRoute
import com.amap.api.toBaiduDirection
import com.amap.api.toTenCentRoute
import com.bumptech.glide.Glide
import com.jeremyliao.liveeventbus.LiveEventBus
import com.jeremyliao.liveeventbus.utils.observe
import com.lzg.extend.jackson.JsonUtil
import com.rzico.sbl.R
import com.rzico.sbl.databinding.ActivityDeliverDetailBinding
import com.rzico.sbl.model.BaseUrl
import com.rzico.sbl.model.OrderBarrelData
import com.rzico.sbl.model.OrderGoods
import com.rzico.sbl.other.OrderService
import com.rzico.sbl.other.privacyStatus
import com.rzico.sbl.other.serviceId
import com.rzico.sbl.other.userRole
import com.rzico.sbl.ui.member.MemberDetailActivity
import com.rzico.sbl.ui.member.MemberTicketActivity
import com.rzico.sbl.viewmodel.DeliverViewModel
import com.xinnuo.common.decoration.SpaceTBDecoration
import com.xinnuo.common.extend.*
import com.xinnuo.common.helper.*
import com.xinnuo.common.view.FullyGridLayoutManager
import com.xinnuo.common.view.FullyLinearLayoutManager
import com.xinnuo.common_ui.base.BaseActivity
import com.xinnuo.common_ui.base.addItems
import com.xinnuo.common_ui.glideExt.loadImage
import com.xinnuo.common_ui.glideExt.loadRoundedImage
import com.xinnuo.common_ui.utils.*
import com.xinnuo.slimadapter.SlimAdapter
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import net.mikaelzero.mojito.ext.mojito
import net.mikaelzero.mojito.impl.DefaultPercentProgress
import net.mikaelzero.mojito.impl.NumIndicator
import net.mikaelzero.mojito.interfaces.IProgress
import net.mikaelzero.mojito.loader.InstanceLoader
import org.json.JSONArray
import org.json.JSONObject

class DeliverDetailActivity : BaseActivity(R.layout.activity_deliver_detail) {

    private val mBinding by lazy { ActivityDeliverDetailBinding.bind(getRootView()) }

    private var mCardId = ""      //会员卡ID
    private var mMemberId = ""    //会员ID
    private var mStatus = ""      //运单状态
    private var mOrderSn = ""     //订单编号
    private var mShippingSn = ""  //运单编号
    private var mOrderMethod = "" //订单类型
    private var mHopeDate = ""     //预约时间
    private var mEstimateDate = "" //预计送达

    private var mReceivePhone = ""  //收货电话
    private var mReceiveAdress = "" //收货地址
    private var mReceiveLat = ""    //收货纬度
    private var mReceiveLng = ""    //收货经度

    // 图片列表
    private var mPhotoList = ArrayList<String>()

    override fun getViewModel() = get<DeliverViewModel>()

    override fun initData() {
        initTitle(title = "运单详情", name = "打印")

        initLayout()
        initPhotoLayout()

        getData()
    }

    private fun initLayout() {
        mBinding
            .detailList
            .apply {
                isNestedScrollingEnabled = false
                layoutManager = FullyLinearLayoutManager(getIContext())

                addItemDecoration(
                    SpaceTBDecoration(
                        top = 5f,
                        hasHeader = false,
                        hasFooter = false,
                        spaceColor = R.color.white
                    )
                )

                mAdapter = SlimAdapter.creator()
                    .register<OrderGoods>(R.layout.item_order_detail_list) { jector, bean, _ ->
                        jector.apply {
                            text(R.id.item_order_name, bean.name)
                            text(R.id.item_order_type, "规格：${bean.spec.orEmpty()}")
                            text(R.id.item_order_price, "")
                            text(R.id.item_order_num, "x${bean.quantity}")

                            visibility(
                                R.id.item_order_img_label,
                                visibleValue { bean.first }
                            )

                            with<ImageView>(R.id.item_order_img) {
                                it.loadImage(bean.thumbnail)
                            }

                            with<ImageView>(R.id.item_order_img_type) {
                                when (mOrderMethod) {
                                    "offline" -> {
                                        it.visible()
                                        it.setImageResource(R.mipmap.icon_order03)
                                    }

                                    "eleme", "elemengc" -> {
                                        it.visible()
                                        it.setImageResource(R.mipmap.icon_order04)
                                    }

                                    "meituan" -> {
                                        it.visible()
                                        it.setImageResource(R.mipmap.icon_order05)
                                    }

                                    "nongfu" -> {
                                        it.visible()
                                        it.setImageResource(R.mipmap.icon_order06)
                                    }

                                    "jd" -> {
                                        it.visible()
                                        it.setImageResource(R.mipmap.icon_order10)
                                    }

                                    "taobao" -> {
                                        it.visible()
                                        it.setImageResource(R.mipmap.icon_order11)
                                    }

                                    "pinduoduo" -> {
                                        it.visible()
                                        it.setImageResource(R.mipmap.icon_order12)
                                    }

                                    "dummy" -> {
                                        it.visible()
                                        it.setImageResource(R.mipmap.icon_order22)
                                    }

                                    "ali" -> {
                                        it.visible()
                                        it.setImageResource(R.mipmap.icon_order26)
                                    }

                                    else -> it.gone()
                                }
                            }

                            with<ImageView>(R.id.item_order_img_status) {
                                it.isVisible = mHopeDate.isNotEmpty()

                                mHopeDate.notEmpty {
                                    it.setImageResource(
                                        conditionIf(
                                            longValue < mEstimateDate.longValue,
                                            R.mipmap.icon_order07,
                                            R.mipmap.icon_order02
                                        )
                                    )
                                }
                            }
                        }
                    }
                    .attachTo(this)
            }

        tvRight?.oneClick {
            mShippingSn.notEmpty {
                showHintDialog(
                    title = "运单打印",
                    hint = "确定要打印该运单吗？",
                    onSure = {
                        getViewModel()
                            .operateOrder(
                                BaseUrl.deliveryPrt,
                                this,
                                false
                            )
                            .bindLifeCycle(getLifecycleOwner())
                            .subscribeBySuccess(
                                event = getViewModel(),
                                onSuccess = {
                                    showToast("订单打印成功！")
                                }
                            )
                    }
                )
            }
        }
    }

    private fun initPhotoLayout() {
        mBinding.apply {
            detailPhoto.also { layout ->
                layout.isNestedScrollingEnabled = false
                layout.layoutManager = FullyGridLayoutManager(baseContext, 3)
                layout.adapter = SlimAdapter.creator()
                    .register<String>(R.layout.item_photo_grid) { jector, bean, index ->
                        jector.apply {
                            gone(R.id.item_photo_del)

                            with<ImageView>(R.id.item_photo_img) {
                                it.loadRoundedImage(
                                    url = bean,
                                    radius = 10f
                                )
                            }

                            clicked {
                                layout.mojito(R.id.item_photo_img) {
                                    urls(mPhotoList)
                                    position(index, headerSize = 1, footerSize = 1)
                                    autoLoadTarget(false)
                                    setProgressLoader(object : InstanceLoader<IProgress> {
                                        override fun providerInstance(): IProgress {
                                            return DefaultPercentProgress()
                                        }
                                    })
                                    progressLoader { DefaultPercentProgress() }
                                    setIndicator(NumIndicator())
                                }
                            }
                        }
                    }
                    .attachTo(layout)
            }

            (detailPhoto.adapter as SlimAdapter).setDataList(mPhotoList)
        }
    }

    override fun initListener() {
        mOrderSn = intent.getExtra("orderSn")
        mShippingSn = intent.getExtra("shippingSn")
        mStatus = intent.getExtra("status")

        mBinding.apply {
            detailCall.oneClick {
                mReceivePhone.notEmpty {
                    showBottomPhoneDialog(this)
                }
            }

            detailAdress.longClick {
                mReceiveAdress.notEmpty {
                    copyStringToClip(this)
                    showToast("地址复制成功！")
                }
            }

            detailAdressCopy.oneClick {
                copyStringToClip(
                    "${detailName.textTrim}\n$mReceiveAdress"
                )
                showToast("复制成功！")
            }

            detailTicketLl.oneClick {
                mCardId.notEmpty {
                    startActivityEx<MemberTicketActivity>(
                        "cardId" to mCardId,
                        "memberId" to mMemberId
                    )
                }
            }

            detailNum.longClick {
                mOrderSn.notEmpty {
                    copyStringToClip(this)
                    showToast("订单编号复制成功！")
                }
            }

            detailNumCopy.oneClick {
                mOrderSn.notEmpty {
                    copyStringToClip(this)
                    showToast("订单编号复制成功！")
                }
            }

            detailMember.oneClick {
                startActivityEx<MemberDetailActivity>(
                    "cardId" to mCardId,
                    "memberId" to mMemberId
                )
            }

            detailYueEdit.oneClick {
                val originTime = mEstimateDate.getTimeString()
                val initYear = TimeHelper
                    .getInstance()
                    .run {
                        getYearOfDate(stringDateShort)
                    }

                showTimeDialog(
                    maxYearValue = initYear,
                    count = 5,
                    title = "修改预计送达时间",
                    isBeforeLimited = true
                ) { _, _, _, _, _, hint ->
                    if ("${hint}:00".isBefore(
                            originTime,
                            "yyyy-MM-dd HH:mm:ss"
                        )
                    ) {
                        lifecycleScope.launch {
                            delay(400)

                            showOneHintDialog(
                                hint = "修改时间不能早于预计送达时间\n${originTime}"
                            )
                        }
                    } else {
                        getViewModel()
                            .requestBind(
                                requestUrl = BaseUrl.deliveryUpe,
                                params = arrayOf(
                                    "sn" to mOrderSn,
                                    "estimateDate" to "${hint}:00"
                                )
                            )
                            .bindLifeCycle(getLifecycleOwner())
                            .subscribeBySuccess(
                                event = getViewModel(),
                                onSuccess = {
                                    showToast("修改成功！")

                                    detailYue.text = "${hint}:00"

                                    mEstimateDate =
                                        TimeHelper
                                            .getInstance()
                                            .stringToLong("${hint}:00")
                                            .toString()
                                }
                            )
                    }
                }
            }

            detailBtn1.oneClick {
                when (mStatus) {
                    "unconfirmed" ->
                        startActivityEx<DeliverSendActivity>(
                            "mode" to "unconfirmed",
                            "orderSn" to mOrderSn,
                            "shippingSn" to mShippingSn
                        )

                    "undispatch" -> {
                        getViewModel()
                            .operateOrder(BaseUrl.deliveryCfm, mShippingSn)
                            .bindLifeCycle(getLifecycleOwner())
                            .subscribeBySuccess(
                                event = getViewModel(),
                                onSuccess = {
                                    showToast("操作成功！")

                                    // 更新运单列表
                                    LiveEventBus
                                        .get("deliverList")
                                        .post("refresh")

                                    finishView()
                                }
                            )
                    }

                    "dispatch" ->
                        startActivityEx<DeliverDispatchActivity>(
                            "shipId" to intent.getExtra("shipId"),
                            "shippingSn" to mShippingSn
                        )

                    "delivery" ->
                        startActivityEx<DeliverReviewActivity>(
                            "shipId" to intent.getExtra("shipId"),
                            "shippingSn" to mShippingSn
                        )
                }
            }

            detailBtn2.oneClick {
                when (mStatus) {
                    "dispatch" ->
                        (mReceiveLat.isNotEmpty() && mReceiveLng.isNotEmpty())
                            .condition {
                                when {
                                    isAvilible("com.autonavi.minimap") -> {
                                        toAMapRoute(
                                            sourceApplication = getString(R.string.app_name),
                                            t = "3",
                                            rideType = "elebike",
                                            dlat = mReceiveLat,
                                            dlon = mReceiveLng
                                        )
                                    }

                                    isAvilible("com.baidu.BaiduMap") -> {
                                        toBaiduDirection(
                                            destination = "$mReceiveLat,$mReceiveLng",
                                            src = "com.rzico.sbl",
                                            mode = "riding",
                                            coord_type = "gcj02"
                                        )
                                    }

                                    isAvilible("com.tencent.map") -> {
                                        toTenCentRoute(
                                            type = "bike",
                                            tocoord = "$mReceiveLat,$mReceiveLng"
                                        )
                                    }

                                    else -> showToast("请安装地图软件！")
                                }
                            }
                }
            }
        }

        getViewModel()
            .mTicketEvent
            .observe(this) {
                mBinding.detailTicket.text = it.toString()
            }

        observe<String>("deliverDetail") {
            mStatus = ""
            getData()
        }
    }

    /**
     * 订单详情
     */
    override fun getData(index: Int, isLoading: Boolean) {
        getViewModel()
            .deliverDet(mShippingSn)
            .bindLifeCycle(getLifecycleOwner())
            .subscribeBySuccess(
                event = getViewModel(),
                onSuccess = { result ->
                    val obj = JSONObject(result)
                    mCardId = obj.optNotNull("cardId")
                    mMemberId = obj.optNotNull("memberId")
                    mHopeDate = obj.optNotNull("hopeDate")
                    mOrderMethod = obj.optNotNull("orderMethod")
                    val sellerId = obj.optNotNull("sellerId")
                    val shopOwnerId = obj.optNotNull("shopOwnerId")
                    val adminMemberId = obj.optNotNull("adminMember")

                    mBinding.apply {
                        // 美团订单金额判断
                        detailShiLl.isVisible = mOrderMethod != "meituan"
                        detailMeiLl.isVisible = mOrderMethod == "meituan"

                        detailAmount.text = obj.optNotNull("price").formatDecimal()

                        (sellerId == shopOwnerId)
                            .also {
                                detailAmountLl.isVisible = it
                                detailShiLl.isVisible = it
                                detailTicketLl.isVisible = it
                            }
                            .condition {
                                // 电子水票
                                detailShui.text = "–${
                                    obj.optNotNull("exchangeDiscount")
                                        .also {
                                            detailShuiLl.isVisible = it.doubleValue > 0
                                        }.formatDecimal()
                                } 元"
                                // 金币
                                detailCoin.text = "–${
                                    obj.optNotNull("pointDiscount")
                                        .also {
                                            detailCoinLl.isVisible = it.doubleValue > 0
                                        }.formatDecimal()
                                } 元"
                                // 优惠券
                                detailYou.text = "–${
                                    obj.optNotNull("couponDiscount")
                                        .also {
                                            detailYouLl.isVisible = it.doubleValue > 0
                                        }.formatDecimal()
                                } 元"
                                // 活动优惠
                                detailHuo.text = "–${
                                    obj.optNotNull("activityDiscount")
                                        .also {
                                            detailHuoLl.isVisible = it.doubleValue > 0
                                        }.formatDecimal()
                                } 元"
                                // 单品优惠
                                detailDan.text = "–${
                                    obj.optNotNull("dragonDiscount")
                                        .also {
                                            detailDanLl.isVisible = it.doubleValue > 0
                                        }.formatDecimal()
                                } 元"
                                // 会员折扣
                                detailHui.text = "–${
                                    obj.optNotNull("vipDiscount")
                                        .also {
                                            detailHuiLl.isVisible = it.doubleValue > 0
                                        }.formatDecimal()
                                } 元"
                                // 客服调价
                                detailTiao.text = "–${
                                    obj.optNotNull("offsetAmount")
                                        .also {
                                            detailTiaoLl.isVisible = it.doubleValue != 0.0
                                        }.replace("-", "")
                                } 元"
                                // 空桶押金
                                detailBucket.text = "+${
                                    obj.optNotNull("pledgePayable")
                                        .also {
                                            detailBucketLl.isVisible = it.doubleValue > 0
                                        }.formatDecimal()
                                } 元(数量: ${obj.optNotNull("pledgeQuantity")})"
                                // 运费
                                detailFee.text = "+${
                                    obj.optNotNull("freight")
                                        .also {
                                            detailFeeLl.isVisible = it.doubleValue > 0
                                        }.formatDecimal()
                                } 元"

                                mCardId.notEmpty {
                                    getViewModel().ticketCount(this)
                                }
                            }

                        obj.optNotNull("amountPayable").also {
                            // 付款金额
                            detailShi.text = it.formatDecimal()
                            // 美团实付金额
                            detailMeiFu.text = it.formatDecimal()
                        }
                        // 美团实收金额
                        detailMeiShou.text = obj.optNotNull("amountPaid").formatDecimal()

                        // 结算金额
                        detailJie.text =
                            obj.optNotNull("settlementPrice")
                                .also {
                                    detailJieLl.isVisible = it.doubleValue > 0.0

                                    (it.doubleValue > 0.0)
                                        .condition {
                                            detailMeiLl.gone()
                                        }
                                }.formatDecimal()

                        /*---------------------- 客户信息 ----------------------*/
                        val objReceive = obj.optJSONObject("receiver") ?: JSONObject()
                        mReceivePhone = objReceive.optNotNull("phone")
                        mReceiveAdress = objReceive.optNotNull("areaName")
                        mReceiveAdress += objReceive.optNotNull("address")
                        mReceiveLat = objReceive.optNotNull("lat")
                        mReceiveLng = objReceive.optNotNull("lng")
                        // 姓名电话
                        detailName.text = "${objReceive.optNotNull("consignee")}  ${
                            when {
                                userRole in "1,2"
                                        || privacyStatus == "off" -> mReceivePhone

                                else -> mReceivePhone.phoneReplaceWithStar()
                            }
                        }"
                        // 收货地址
                        detailAdress.text = mReceiveAdress
                        // 查看会员
                        detailMember.isVisible =
                            userRole in "1,2"
                                    && mCardId.isNotEmpty()
                                    && mMemberId.isNotEmpty()
                        // 买家留言
                        detailMai.text =
                            obj.optNotNull("orderMemo")
                                .notEmpty { detailMaiLl.visible() }
                        // 商家备注
                        detailCmemo.text =
                            obj.optNotNull("cardMemo")
                                .notEmpty { detailCmemoLl.visible() }

                        /*---------------------- 订单信息 ----------------------*/
                        // 订单状态
                        detailStatus.text = obj.optNotNull("statusDescr")
                        // 订单编号
                        detailNum.text = mOrderSn
                        // 订单类型
                        detailType.text =
                            obj.optNotNull("orderMethod")
                                .run {
                                    mOrderMethod = this
                                    notEmpty { detailTypeLl.visible() }

                                    when (this) {
                                        "online" ->
                                            when (obj.optNotNull("paymentPluginId")) {
                                                "ticketPayPlugin" -> "货到付款"
                                                else -> "线上订单"
                                            }

                                        "ali" ->
                                            when (obj.optNotNull("paymentPluginId")) {
                                                "ticketPayPlugin" -> "货到付款"
                                                else -> "支付宝"
                                            }

                                        "offline" -> "人工报单"
                                        "history" -> "人工补单"
                                        "nongfu" -> "农夫APP"
                                        "meituan" -> "美团"
                                        "eleme", "elemengc" -> "饿了么"
                                        "jd" -> "京东"
                                        "taobao" -> "淘宝"
                                        "pinduoduo" -> "拼多多"
                                        else -> "其他"
                                    }
                                }
                        // 支付状态
                        detailMethod.text =
                            when (mOrderMethod) {
                                "offline" ->
                                    obj.optNotNull("payStatus")
                                        .run {
                                            when (this) {
                                                "unpaid" -> {
                                                    obj.optNotNull("payMethod")
                                                        .run {
                                                            isEmpty()
                                                                .conditionElseValue(
                                                                    actionTrue = {
                                                                        detailMethodLl.visible()
                                                                        "未支付"
                                                                    },
                                                                    actionFalse = { "已支付" }
                                                                )
                                                        }
                                                }

                                                else -> "已支付"
                                            }
                                        }

                                "online", "ali" ->
                                    obj.optNotNull("payStatus")
                                        .run {
                                            when (this) {
                                                "unpaid" -> {
                                                    detailMethodLl.visible()
                                                    "未支付"
                                                }

                                                else -> "已支付"
                                            }
                                        }

                                else -> "已支付"
                            }
                        // 支付方式
                        detailPay.text =
                            obj.optNotNull("payMethodName")
                                .also {
                                    detailPayLl.isGone = it.isEmpty()
                                }
                        // 电子签单
                        obj.optNotNull("signature")
                            .notEmpty {
                                detailSignLl.visible()

                                Glide.with(getIContext())
                                    .load(this)
                                    .into(detailSign)
                            }
                        // 销售站点
                        (mOrderMethod == "meituan")
                            .conditionElse(
                                actionTrue = {
                                    detailShou.text =
                                        obj.optNotNull("meituanShopName")
                                            .notEmpty { detailShouLl.visible() }
                                },
                                actionFalse = {
                                    detailShou.text =
                                        obj.optNotNull("sellerName")
                                            .notEmpty { detailShouLl.visible() }
                                }
                            )
                        // 下单时间
                        detailCreate.text =
                            obj.optNotNull("orderDate")
                                .getTimeString()
                        // 预计送达
                        detailYue.text =
                            obj.optNotNull("estimateDate")
                                .also { mEstimateDate = it }
                                .notEmpty { detailYueLl.visible() }
                                .getTimeString()
                        // 发货时间
                        detailFa.text =
                            obj.optNotNull("shippingDate")
                                .notEmpty { detailFaLl.visible() }
                                .getTimeString()
                        // 送达时间
                        detailDa.text =
                            obj.optNotNull("deliveryDate")
                                .notEmpty { detailDaLl.visible() }
                                .getTimeString()
                        // 交易完成
                        detailWan.text =
                            obj.optNotNull("completeDate")
                                .notEmpty { detailWanLl.visible() }
                                .getTimeString()
                        // 派单留言
                        detailPai.text =
                            obj.optNotNull("memo")
                                .trimEnd('\n')
                                .notEmpty { detailPaiLl.visible() }
                        // 配送方式
                        detailDeliver.text =
                            obj.optNotNull("shippingMethod")
                                .notEmpty { detailDeliverLl.visible() }
                        // 配送站点
                        detailZhan.text =
                            obj.optNotNull("shopName")
                                .notEmpty { detailZhanLl.visible() }
                        // 配送人员
                        detailRen.text =
                            (obj.optNotNull("adminName")
                                    + obj.optNotNull("adminPhone").ifNotEmpty { " - $it" }
                                    )
                                .notEmpty { detailRenLl.visible() }
                        // 配送费用
                        detailPei.text =
                            obj.optNotNull("shippingFreight")
                                .also {
                                    detailPeiLl.isVisible = it.doubleValue > 0
                                }.formatDecimal()
                        // 送货工资
                        detailSong.text =
                            obj.optNotNull("adminFreight")
                                .also {
                                    detailSongLl.isVisible = it.doubleValue > 0
                                }.formatDecimal()

                        /*---------------------- 回桶明细 ----------------------*/
                        val objBarrel = obj.optJSONArray("shippingBarrels") ?: JSONArray()
                        val barrelList = arrayListOf<OrderBarrelData>()
                        barrelList.addItems(
                            JsonUtil.toList(
                                objBarrel.toString(),
                                OrderBarrelData::class.java
                            )
                        )
                        detailDispatch.text =
                            barrelList
                                .also {
                                    detailDispatchLl.isVisible = it.isNotEmpty()
                                }
                                .joinToString("\n") {
                                    "${it.name} | 送出${it.quantity}桶 | 收回${it.returnQuantity}桶"
                                }
                    }

                    /*---------------------- 商品信息 ----------------------*/
                    val objOrder = obj.optJSONArray("shippingItems") ?: JSONArray()
                    mList.clear()
                    mList.addItems(
                        JsonUtil.toList(
                            objOrder.toString(),
                            OrderGoods::class.java
                        )
                    )

                    // 是否首单
                    val isFirst = obj.optBoolean("isFirst")
                    mList.forEach { (it as OrderGoods).first = isFirst }

                    mAdapter.setDataList(mList)

                    /*---------------------- 拍照记录 ----------------------*/
                    val objPhoto = obj.optJSONArray("shippingPhotos") ?: JSONArray()
                    mPhotoList.clear()
                    mPhotoList.addItems(
                        JsonUtil.toList(
                            objPhoto.toString(),
                            String::class.java
                        )
                    )
                    mPhotoList.notEmpty {
                        mBinding.detailPhotoLl.visible()
                        (mBinding.detailPhoto.adapter as SlimAdapter).notifyDataSetChanged()
                    }

                    // 商品数量
                    mBinding
                        .detailTotal
                        .text = kotlin.run {
                        var count = 0
                        mList.forEach {
                            it as OrderGoods
                            count += it.quantity.intValue
                        }
                        count.toString()
                    }

                    // 拨号按钮显隐
                    val shipStatus = obj.optNotNull("status")
                    mBinding
                        .detailCall
                        .isVisible =
                        userRole in "1,2"
                                || (shipStatus == "shipped" && mBinding.detailDispatchLl.isGone)

                    if (userRole in "1,2,3,10") {
                        mBinding.apply {
                            when (mStatus) {
                                "unconfirmed" -> {
                                    detailBottom
                                        .isVisible =
                                        (userRole in "1,2,10")
                                            .apply {
                                                detailBtn1.text = "派单"
                                                detailBtn1.visible()
                                                detailBtn2.gone()
                                                detailBtn3.gone()
                                            }
                                }

                                "undispatch" -> {
                                    detailBottom
                                        .isVisible =
                                        (shipStatus == "shipped")
                                            .apply {
                                                detailBtn1.text = "确认配送"

                                                detailBtn2.gone()
                                                detailBtn3.gone()
                                                detailBtn1.isVisible =
                                                    getString("uid") == adminMemberId
                                                detailYueEdit.isVisible =
                                                    this && serviceId in OrderService.MODIFY_TIME
                                            }
                                }

                                "dispatch" -> {
                                    detailBottom
                                        .isVisible =
                                        (shipStatus == "shipped")
                                            .apply {
                                                detailBtn1.text = "送达"
                                                detailBtn2.text = "一键导航"

                                                detailBtn3.gone()
                                                detailBtn1.isVisible =
                                                    getString("uid") == adminMemberId
                                                detailBtn2.isGone =
                                                    mReceiveLat.isEmpty() || mReceiveLng.isEmpty()
                                                detailYueEdit.isVisible =
                                                    this && serviceId in OrderService.MODIFY_TIME
                                            }
                                }

                                "delivery" -> {
                                    detailBottom
                                        .isVisible =
                                        (shipStatus == "delivery" && userRole in "1,2")
                                            .apply {
                                                detailBtn1.text = "审核"
                                                detailBtn1.visible()
                                                detailBtn2.gone()
                                                detailBtn3.gone()
                                            }
                                }

                                else -> detailBottom.gone()
                            }
                        }
                    }
                }
            )
    }

}